<?
    require_once('config.php');
    
    if ($_POST) {
        if (AuthManager::Authenticate($_POST['username'],$_POST['password'])) {
            $menu = new Menu('Getting Started');
            if (AuthManager::AuthLevelMatches(AuthConstants::ADMINISTRATORS)) { 
                $newguests = DBM::CountRows("SELECT * FROM guests WHERE approved=0;");
                $menu->AddItem(new MenuItem("Approve Guests" . ($newguests ? " <b>($newguests)</b>" : ""),"admin_guest.php"));
                $menu->AddItem(new MenuItem("Examine New Listings","admin_listings.php"));
                
                MenuManager::AddMenu("Authentication Successful",$menu);
            }
            else if (AuthManager::AuthLevelMatches(array(AuthConstants::LANDLORDS))) {
                $menu->AddItem(new MenuItem("Edit Profile","profile.php?action=edit"));
                $menu->AddItem(new MenuItem("Edit My Listings","listings.php"));
                
                MenuManager::AddMenu("Authentication Successful",$menu);
                
                $query = "SELECT * FROM guests JOIN landlords ON landlords.accountid = guests.id WHERE approved=1 AND guests.id = " . AuthManager::GetUserID($_POST['username']) . ";";
                $res = DBM::FetchRow($query);
                
                if (!is_array($res)) {
                    Functions::PrintHeader("Authentication Successful");
                    ?><div style='text-align:center'><img src='<?=ThemeManager::GetImage("Unlocked")?>' align='center' /></div><br><?
                    SuccessMessage::Show("Great! Now, Create Your Profile","You have been successfully logged in.  As a landlord, you are required to create a profile in order for perspective tenants to contact you.  <a href='profile.php?action=edit'>Click Here</a> to begin filling out your profile.");
                    Functions::PrintFooter();
                    exit;
                }
            }
            else {
                $menu->AddItem(new MenuItem("View Listings","browse.php"));
                $menu->AddItem(new MenuItem("Search Listings","search.php"));
                
                MenuManager::AddMenu("Authentication Successful",$menu);
            }
            
            Functions::PrintHeader("Authentication Successful");
            ?><div style='text-align:center'><img src='<?=ThemeManager::GetImage("Unlocked")?>' align='center' /></div><br><?
            SuccessMessage::Show("Authentication Successful","You have been successfully logged in.  Use the links on the left to get started.");
            Functions::PrintFooter();
        }
        else {
            header("Location: login.php?error=" . AuthManager::GetError());
        }
    }
    else if ($_GET['method'] == "cas") {
    	require_once("include/auth/CAS/CAS.php");
    	
    	phpCAS::client(CAS_VERSION_2_0,'login.rpi.edu',443,'/cas',false);
    	phpCAS::setNoCasServerValidation();
    	phpCAS::forceAuthentication();
    	
		$user = phpCAS::getUser();
		if (isset($user)) {
			// do this
			if (!AuthManager::Authenticate($user,NULL,true)) {
				Functions::PrintHeader("Login Failed");
				ErrorMessage::Show("Login Failed","Auth Login failed.  Please try again.");
				Functions::PrintFooter();
			}
			else {
				header("Location: index.php");
				exit;
			}
		}
		else {
			Functions::PrintHeader("Login Failed");
			ErrorMessage::Show("Login Failed","CAS Login failed.  Please try again.");
			Functions::PrintFooter();
		}
    }
    else {
        Functions::PrintHeader("Login");
        
        if ($_GET['error']) {
            $error = AuthManager::GetErrorDetails($_GET['error']);
            $error->Show();
        }
        
        WarningMessage::Show("RPI Students,Faculty, and Staff","<a href='login.php?method=cas'>If you're an RPI student, faculty, or staff, click here to login with your RCS account.</a><br /><br />If you are a faculty or staff member who wants to post listings, then you need to create a landlord account <a href='guest.php?type=landlord'>here</a>, as your RCS id will only allow you to <i>view</i> listings.");
        ?>
        <br />
        <form method="POST" action="login.php">
            <table align='center' class='cgltable'>
            <thead>
                <tr>
                    <th colspan='2'>Login</th>
                </tr>
                </thead>
                <tr>
                    <td colspan='2' style='text-align:center;'>
                        <?
                            if (SessionManager::Get("authenticated")) 
                                echo "<img src='" . ThemeManager::GetImage("unlocked") . "' /><br><span style='font-weight:bold;color:337744;'>You are already logged in</span>";
                            else 
                                echo "<img src='" . ThemeManager::GetImage("locked") . "' />";
                        ?><br />
                    </td>
                </tr>
                <tr>
                    <td colspan='2'>
                        Need an account? <a href='guest.php'>Guest Account</a> | <a href='guest.php?type=landlord'>Landlord Account</a><br />
                        <br />
                        Already have a landlord account in the old system?  <a href='migrate.php'>Migrate it Here</a>.
                    </td>
                </tr>
                <tr>
                    <td class='label'>E-Mail</td>
                    <td><input type='text' name='username' /></td>
                </tr>
                <tr>
                    <td class='label'>Password</td>
                    <td>
		    	<input type='password' name='password' />
			<br />
		    </td>
                </tr>
                <tfoot>
                <tr>
                    <td colspan='2'>
                        <input type='reset' value='Start Over' /><input type='submit' value='Login' />
                    </td>
                </tr>
                </tfoot>
            </table>
            <br />
            <div style='text-align:center;'>
                <a href='changepassword.php?action=forgot'>Forget your password?</a><br />
            </div>
            <br />
            <br />
        </form>
        <?
        Functions::PrintFooter();
    }
?>
